<?php

class OrderAction extends ActionBase {
	public function run($id) {
		$model = VGoodsAgentable::model()->with('goods')->findByPk($id);
		$data = array(
			'pay_type' => Order::PAY_TYPE_OFFLINE,
			'items' => array(
				'related_id' => $model->related_id,
				'goods_id' => $model->goods_id,
				'provider_id' => $model->provider_id,
				'dealer_id' => $model->dealer_id,
				'item_num' => 1,
				'item_price' =>$model->goods->getTurnoverPrice(),
			),
		);
		$order = Order::createOrder($model->goods->class_id, $data);
	
		if($order->save()) {
			$message = '商品：{goods} 下单成功，请完善订单信息！';
			$url = $this->controller->createUrl('order/view', array(
				'id' => $order->id,
			));
			$link = CHtml::link('点此处查看该订单', $url);
			Yii::app()->user->setFlash(FLASH::RESULT_SUCCESS, str_replace(
				array('{goods}', '{orderUrl}'), array($model->goods->title, $link), $message)
			);
			Yii::app()->controller->redirect(Yii::app()->controller->createUrl(get_class($order).'/edit', array(
				'id' => $order->id,
			)));
		} else {
			Yii::app()->user->setFlash(FLASH::RESULT_FAILED, '商品：'.$model->goods->title.' 下单失败！');
		}
		CControllerHelper::redirectReferrer();
	}
}

?>